package com.eastcode.mobile.server.user.action;

import java.util.List;

import com.eastcode.base.action.BaseAction;
import com.eastcode.mobile.server.system.entity.User;
import com.eastcode.mobile.server.user.entity.Location;
import com.eastcode.mobile.server.user.service.LocationService;
import com.eastcode.mobile.server.user.view.LocationView;
import com.eastcode.mobile.server.util.ServerConstant;
import com.eastcode.mobile.server.util.ServerLocator;

public class LocationAction extends BaseAction {

	private static final long serialVersionUID = -1889361750349194567L;

	private LocationService locationService = ServerLocator.getLocationService();

	public void execute() {
		prepare(this);
	}

	/**
	 * 查询数据
	 * 
	 */
	public void search() {
		LocationView view = parseRequest(LocationView.class, "view.");

		User user = (User)session.getAttribute(ServerConstant.SYS_USER);
		view.getParamMap().put("userId", user.getUserId());
		
		List<Location> result = locationService.searchList(Location.class, view);
		view.setResult(result);

		request.setAttribute("view", view);

		forward("/template/client/location/list.ftl");
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public void delete() {
		long id = Long.parseLong(request.getParameter("view.id"));

		locationService.deleteObjectByKey(Location.class, id);
		search();
	}

	/**
	 * 删除所选的数据
	 */
	public void deleteAll() {
		LocationView view = parseRequest(LocationView.class, "view.");

		locationService.deleteObjectBykeys(Location.class, view.getIds());

		search();
	}

	/**
	 * 查看详情
	 * 
	 */
	public void detail() {
		long id = Long.parseLong(request.getParameter("view.id"));
		Location location = locationService.findObjectByKey(Location.class, id);

		setResult(location);

		LocationView view = parseRequest(LocationView.class, "view.");
		view.setId(id);
		request.setAttribute("view", view);

		forward("/template/client/location/detail.ftl");
	}

}
